# Copyright (c) 2005,2006,2007 Dynamic Solutions Inc. (support@dynamic-solutions.com)
#
# This file is part of DataZoomer.
#
# DataZoomer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# DataZoomer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Displays web content"""


import os.path

import dzapp, dzpage, dzml, dzutil, dzentity, dzfill

class View(dzentity.Entity):
    def get_SET(self,name,value):
        if name=='title':
            print 'set title'
            #self.title = value
        elif name=='subtitle':
            print 'set subtitle'
#            self.obj.subtitle = value
        else:
            return '%s=%s' % (name,value)
        return ''

# from Wiki - need a general purpose routine
def calc_name(name):
    if name:
        # Take out any nasty characters
        new_name = []
        legal_chars = 'abcdefghijklmnopqrstuvwxyz0123456789_. '
        for c in name:
            if c in legal_chars:
                new_name.append(c)
        return ''.join(new_name).strip().replace(' ','_')

class CustomApp(dzapp.App):
    def view_page(self,page_name='index'):
        def handle(tag,*args):
            return dzutil.link_to(args[1],page=calc_name(args[0]))

        path = os.path.join(zoomer.data_path,'content','pages','%s.htm' % page_name)
        if os.path.exists(path):
            t = open(path,'r').read()
        else:
            path = os.path.join(zoomer.data_path,'content','pages','%s.txt' % page_name)
            if os.path.exists(path):
                t = dzml.dzml(open(path,'r').read(),callback=handle)
            else:
                t = 'Error, unknown page: <i>%s</i><br>' % (page_name)

        page = dzpage.Page(t)
        page.callback = handle
        page.title = ''

        return page

    #---- Other
    def _view_(self):
        return self.view_page(zoomer.webvars.page or 'index')


result = CustomApp('content')

if __name__ == '__main__':
    import dzmanager
    mgr = dzmanager.Manager()
    app = CustomApp()
    print app.iconname
    mgr.run_app(app)
    print zoomer.app_response
